﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logistics_Service.DTO;
using System.Data;

namespace Logistics_Service.DAO
{
    public class NhanVienDAO
    {
        public NhanVienDTO[] DanhSachNhanVien()
        {
            NhanVienDTO[] arr;
            string query = string.Format("select * from NhanVien");
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            int n = dt.Rows.Count;
            arr = new NhanVienDTO[n];
            for (int i = 0; i < n; i++)
            {
                object lnv = GetDataFromDataRow(dt, i);
                arr[i] = (NhanVienDTO)lnv;
            }
            return arr;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            NhanVienDTO nv = new NhanVienDTO();
            nv.Ma = dt.Rows[i]["Ma"].ToString();
            nv.Ten = dt.Rows[i]["Ten"].ToString();
            nv.CMND = dt.Rows[i]["CMND"].ToString();
            nv.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            nv.SDT = dt.Rows[i]["SDT"].ToString();
            nv.ChucVu = ChucVuDAO.Get(dt.Rows[i]["MaChucVu"].ToString());
            nv.ChiNhanh = ChiNhanhDAO.Get(dt.Rows[i]["MaChiNhanh"].ToString());
            nv.ThamNien = Int32.Parse(dt.Rows[i]["ThamNien"].ToString());
            nv.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
            nv.PassWord = dt.Rows[i]["PassWord"].ToString();
            return (object)nv;
        }
        public static NhanVienDTO Get(string ma)
        {
            string query = "select * from nhanvien where ma = '" + ma + "'";
            DataProvider provider = new DataProvider();
            NhanVienDTO nv = new NhanVienDTO();

            DataTable dt = provider.ExecuteQuery(query);

            nv.Ma = dt.Rows[0]["Ma"].ToString();
            nv.Ten = dt.Rows[0]["Ten"].ToString();
            nv.CMND = dt.Rows[0]["CMND"].ToString();
            nv.DiaChi = dt.Rows[0]["DiaChi"].ToString();
            nv.SDT = dt.Rows[0]["SDT"].ToString();
            nv.ChucVu = ChucVuDAO.Get(dt.Rows[0]["MaChucVu"].ToString());
            nv.DaXoa = bool.Parse(dt.Rows[0]["DaXoa"].ToString());
            nv.PassWord = dt.Rows[0]["PassWord"].ToString();

            return nv;
        }
    }
}